package com.sailorj.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sailorj.medical.dao.FamilyDao;
import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.family.Family;

public class FamilyServlet extends BaseServlet {
	FamilyDao familyDao = new FamilyDao();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");// 动作类型参数
		if ("list".equals(m)) {
			process(request, response, "/page/family/family_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/family/family_add.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/family/family_edit.jsp");
		} else if ("getFamilyID".equals(m)) {
			process(request, response, "/page/family_member/familyMember_addByFamilyID.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			process(request, response, "/page/family/family_list.jsp");
		} else if ("edit".equals(m)) {
			this.edit(request, response);
		} else if ("del".equals(m)) {
			this.del(request, response);
		}
	}

	private void add(HttpServletRequest rs, HttpServletResponse response) throws ServletException, IOException {
		// 接收页面传递过来的参数
		String familycode = rs.getParameter("familycode");
		String familyname = rs.getParameter("familyname");
		String IDcard = rs.getParameter("IDcard");
		String familyaddr = rs.getParameter("familyaddr");
		String familynumber = rs.getParameter("familynumber");
		String familystate = rs.getParameter("familystate");
		String userid = rs.getParameter("userid");
		String areacode = rs.getParameter("areacode");
		String familyteam = rs.getParameter("familyteam");

		// 实例化Family类
		Family family = new Family();
		// 调用添加方法
		try {
			Area area = new Area(areacode);
			family.addFamily(familycode, familyname, IDcard, familyaddr, familynumber, familystate, userid, area,
					familyteam);
		} catch (Exception e) {
			error(rs, response);
		}

	}

	private void edit(HttpServletRequest rs, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数

		String familycode = rs.getParameter("familycode");
		String familyname = rs.getParameter("familyname");
		String IDcard = rs.getParameter("IDcard");
		String familyaddr = rs.getParameter("familyaddr");
		String familynumber = rs.getParameter("familynumber");
		String familystate = rs.getParameter("familystate");
		String areacode = rs.getParameter("areacode");
		String familyteam = rs.getParameter("familyteam");

		try {
			// 2:构造新慢病信息对象
			Area area = new Area(areacode);
			Family model = new Family(familycode, familyname, IDcard, familyaddr, familynumber, familystate, area,
					familyteam);
			// 3：调用保存的方法
			model.edit();
			process(rs, resp, "/page/family/family_list.jsp");
		} catch (Exception e) {
			error(rs, resp);
		}

	}

	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String familycode = req.getParameter("id");

		// 2:构造新慢病信息对象
		Family model = new Family();
		try {
			model.del(familycode);
			process(req, resp, "/page/family/family_list.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}



}
